import {
    Coupon
} from './coupon-model.js';
var coupon = new Coupon();
//获取应用实例
const app = getApp()
Page({
    /**
     * 页面的初始数据
     **/
    data: {
        showReceive: false,
        showPage: false,
        clickBtn: true,
        isReceive: false
    },
    /*
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        let id = options.id;
        console.log("couponShare-options--------------------", options)
        let inviteCode = "";
        // let code = wx.getStorageSync("inviteCode");

        //判断若为当前页面分享过来，inviteCode为当前的分享人手机号，再从缓存里面取
        if (options && options.inviteCode) {
            inviteCode = options.inviteCode;
            wx.setStorageSync("inviteCode", inviteCode);
        }
        // else if (code!=""){
        //   inviteCode = code;
        // }

        this.setData({
            id,
            inviteCode
        })

        this.initLoad();

    },
    initLoad() {
        // wx.showLoading({
        //   title: '加载中...',
        // })
        let {
            id,
            inviteCode
        } = this.data;
        // let code = wx.getStorageSync("inviteCode")
        let params = {
            "id": parseInt(id),
            "inviteCode": inviteCode
        }
        coupon.getCouponById(params, res => {
            let data = res.data;
            if (res.status) {
                wx.hideLoading();
                let couponData = data.coupon;

                couponData.startTime = coupon.formate(couponData.startTime, 'yyyy-MM-dd')
                couponData.endTime = coupon.formate(couponData.end_time, 'yyyy-MM-dd')
                let promoter = data.promoter;
                this.setData({
                    coupon: couponData,
                    promoter,
                    showPage: true
                })
            }
        });
    },
    closeReceive() {
        console.log('点击了');
        this.setData({
            showReceive: false
        })
    },
    // 立即领取
    receive() {
        let sessionId = wx.getStorageSync('sessionId');
        if (sessionId == "") {
            wx.navigateTo({
                url: '/pages/getUserInfo/getUserInfo',
            })
            return
        }
        //如果已经领取
        console.log(this.data.isReceive);
        if (this.data.isReceive) {
            console.log("跳转到index页面----------");
            wx.switchTab({
                url: '/pages/index/index',
            })

            return
        }

        this.setData({
            clickBtn: false
        })
        wx.showLoading({
            title: '加载中...',
        })
        let params = {
            "couponIds": '' + this.data.coupon.id
        }
        coupon.receive(params, res => {
            let data = res.data;
            if (res.status) {

                wx.hideLoading();

                if (data.length == 0) {
                    wx.showToast({
                        title: '该优惠券已拥有',
                    })
                    this.setData({
                        clickBtn: true,
                        isReceive: true
                    })
                    return
                }
                this.setData({
                    showReceive: true,
                    clickBtn: true,
                    isReceive: true
                })
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none'
                })
            }
        })
    },
    //立即使用
    goIndex() {
        wx.switchTab({
            url: '/pages/index/index'
        })
    }
})